package com.ruoyi.project.vo;


import io.swagger.annotations.ApiModel;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.HashMap;

/**
 * @author abel lee
 * @create 2021-01-18 14:56
 **/
@ApiModel(description = "通用返回数据格式")
public class JSONResult<T> implements Serializable {


    private T data;
    private String msg;
    private HashMap<String, Object> extData;
    /**
     * 定义的业务错误码
     */
    private String businessErrorCode;
    private Integer code;

    public JSONResult() {
        code = HttpStatus.OK.value();
    }

    public JSONResult(T data) {
        this.setCode(0);
        this.setData(data);
    }

    public JSONResult(Integer code, String msg) {
        this.setCode(code);
        this.setMsg(msg);
    }

    public JSONResult(Integer code, String msg, T data) {
        this.setCode(code);
        this.setMsg(msg);
        this.setData(data);
    }

    public void addExtData(String key, Object value) {
        if (this.extData == null) {
            this.extData = new HashMap<>();
        }
        this.extData.put(key, value);
    }


    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public HashMap<String, Object> getExtData() {
        return extData;
    }

    public void setExtData(HashMap<String, Object> extData) {
        this.extData = extData;
    }

    public String getBusinessErrorCode() {
        return businessErrorCode;
    }

    public void setBusinessErrorCode(String businessErrorCode) {
        this.businessErrorCode = businessErrorCode;
    }

    public void setCode(Integer code) {
        this.code = code;
        if (code == HttpStatus.INTERNAL_SERVER_ERROR.value() && data instanceof String) {
            this.setMsg((String) data);
        }
    }
}
